package homework.controller;

import homework.entity.Goods;
import homework.entity.User;
import homework.service.GoodsService;
import homework.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :黄靓仔
 * @description: TODO
 * @data :2020/8/14 13:51
 */
@RestController
@ResponseBody
//@RequestMapping("/user")
public class Controller {
    public static int flag1=0;  //判断是否登录成功,1为成功，0为失败
    public static int flag2=0;   //判断用户是否为管理员，1为管理员，0为非管理员
    //public static int a[]=new int[]{}
    private static User thisUser=null;
    @Autowired
    GoodsService goodsService;

    @Autowired
    UserService userService;

    @ResponseBody
    @PostMapping("/register")
    public String register(@RequestParam String userName,@RequestParam String zhangHao,@RequestParam String miMa){
        User user=userService.register(userName,zhangHao,miMa);
        if(user==null){
            return "注册成功";
        }else{
            System.out.println("hshkahd");
            return "注册失败,已有该账号";
        }
    }

    @ResponseBody
    @GetMapping("/login")
    public Map login(@RequestParam("zhangHao") String zhangHao, @RequestParam("miMa") String miMa) {
        String a = "登录失败,请检查账号或者密码是否正确";
        if (a.equals(userService.login(zhangHao, miMa))) {
            flag1 = 0;
            //System.out.println(a);
            Map<String,Integer> map=new HashMap<String,Integer>();
            map.put("status",-1);
            return map;
        } else {
            flag1 = 1;
            //System.out.println("1");
            thisUser=userService.isAdministrator(zhangHao, miMa);
            System.out.println(thisUser.toString());
            flag2=thisUser.getAdminist();
           // return goodsService.queryAll();     //显示商品信息
            Map<String,Integer> map=new HashMap<String,Integer>();
            //map.put("flag1",flag1);
            map.put("administor",flag2);
            map.put("status",1);
            return map;
        }
    }

    @ResponseBody
    @PutMapping("/buy")
    public String buy(@RequestParam("goodsName") String goodsName,@RequestParam("number") Integer number){
        if(flag1==0){
            return "请先登录";
        }
       // System.out.println(flag1);
       /* if(thisUser==null){
            System.out.println("空对象");
        }*/
        System.out.println("这里是buy");
        System.out.println(thisUser.toString());

        return userService.buy(goodsName, number,thisUser.getZhangHao());
    }

    @ResponseBody
    @PutMapping("/addBalance")  //充值
    public String addBalance( @RequestParam("number")BigDecimal number){
        if(flag1==0){
            return "请先登录";
        }
        int a=number.compareTo(BigDecimal.ZERO);
        if(a==1) {
            userService.add_balance( number, thisUser);
            return "充值成功";
        }else{
            return "充值失败";
        }
    }

    @ResponseBody
    @PostMapping("/addGoodNum")
    public String addGoodNum(@RequestParam("goodsName") String goodsName,@RequestParam("number") int number){
        if(flag1==1 && flag2==1){
            goodsService.changeGoodsNum(goodsName,number);
            return "补充成功";
        }else{
            return "未登录或者没有权限更改";
        }
    }

    @ResponseBody
    @PutMapping("/changGoodsFlag")  //选择上架商品或者是下架商品
    public String  changGoodsFlag(@RequestParam("goodsName") String goodsName,@RequestParam("flag") int flag){
        if(flag1==1 && flag2==1) {
            //flag=goodsService.getGoodsFlag(goodsName);    //注意改这里///hj//hyr//4/5/6
            goodsService.changeGoodsFlag(goodsName, flag);
            return "更改成功";
        }else{
            return "未登录或者没有权限更改";
        }
    }

    @ResponseBody
    @PostMapping("/addGood") //管理员添加商品
    public String addGood(@RequestBody Goods goods){
            if (flag1 == 1 && flag2 == 1) {
                goodsService.addGoods(goods);
                return "添加成功";
            } else {
                return "未登录或者没有权限更改";
            }

    }

    //@ResponseBody
    @GetMapping("/getAllGoods")
    public List<Goods> getAllGoods(){
        if(flag1==0){
            return null;
        }
        return goodsService.queryAll();     //显示商品信息
    }


    /*@ResponseBody
    @GetMapping("/getAllUser")
    //@RequestMapping("/getAllUser")
    public List<User> getAllUser(){
        if(flag1==0){
            return null;
        }
        else {
            return userService.queryAll();//显示用户信息
        }
    }*/

    @GetMapping("/admin")   //判断是否管理员
    public Map admin(){
        Map<String,Integer> map=new HashMap<String,Integer>();
        map.put("administor",flag2);
        return map;
    }

    @PostMapping("/logout")
    public String logout(){
        flag1=0;
        return "已退出该账号";
    }
}
